﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using LiuYan.Entity;
using LiuYan.Service;

namespace LiuYan.Web.Areas.Admin.Controllers
{
    public class ImgArticleController : Controller
    {
        //
        // GET: /Admin/ImgArticle/

        public ActionResult Index()
        {
            return View();
        }
        public string GetList()
        {
            int pageIndex = Convert.ToInt32(Request["pageindex"]);
            int pageCount = Convert.ToInt32(Request["pagecount"]);
            string title = Request["title"];
            return JsonConvert.SerializeObject(new ImgArticleService().GetList(pageIndex, pageCount, title));
        }
        public ActionResult Window(int id)
        {
            ViewData["id"] = id;
            return View();
        }

        /// <summary>
        /// 上传文章封面图片
        /// </summary>
        /// <returns></returns>
        public string UploadImg()
        {
            HttpFileCollection file = System.Web.HttpContext.Current.Request.Files;
            int num = file.Count;
            string imgPath = "";
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string fileName = file[0].FileName.Split('\\').Last();
            imgPath = "/Content/fmimg/" + Convert.ToInt64(ts.TotalSeconds) + "." + fileName.Split('.').Last();
            string path = Server.MapPath(imgPath);
            file[0].SaveAs(path);
            return "{'status':'1','path':'" + imgPath + "'}";
        }
        public int EditArticle(FormCollection form)
        {
            TB_ImgArticle entity = new TB_ImgArticle();
            entity.Id = Convert.ToInt32(form["id"]);
            entity.Del = 0;
            entity.Des = form["des"];
            entity.Content = Server.UrlDecode(form["content"]);
            entity.Img = form["img"];
            entity.Title = form["title"];
            entity.Url = form["url"];
            entity.Date = DateTime.Now;
            return new ImgArticleService().Edit(entity);
        }
        public string GetOneData(int id)
        {
            return JsonConvert.SerializeObject(new BaseService().GetOneData(id, "TB_ImgArticle"));
        }
        public int Del(string ids)
        {
            return new BaseService().Dels(ids, "TB_ImgArticle",false);
        }

    }
}
